﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Configuration;
using Remind.View.Properties;
using System.IO;

namespace Remind.View
{
    public partial class FrmOption : Form
    {
        bool cuspath = false;
        public FrmOption()
        {
            InitializeComponent();
        }
        //
        private void ckbAutoRun_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string startup = Application.ExecutablePath;//取得程序路径 
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装            
            RegistryKey rKey = Registry.LocalMachine;
            RegistryKey autoRun = rKey.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            //判断是否开机自启动
            if (ckbAutoRun.Checked)
            {
                try
                {
                    autoRun.SetValue("Onion", startup);
                    rKey.Close();
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                Settings.Default.autoRun = true;//设置用户配置
            }
            else
            {
                try
                {
                    autoRun.DeleteValue("Onion", false);
                    autoRun.Close();
                    rKey.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                Settings.Default.autoRun = false;
            }
            Settings.Default.runPwd = ckbPwd.Checked;
            Settings.Default.custspath = cuspath;
            Settings.Default.Save();//保存用户设置
            this.Close();
        }

        private void FrmOption_Load(object sender, EventArgs e)
        {
            //读取用户设置
            ckbAutoRun.Checked = Settings.Default.autoRun;
            ckbPwd.Checked = Settings.Default.runPwd;
            //
            textBox1.Text = ConfigurationManager.AppSettings["database"];
        }
        
        private void ckbPwd_CheckedChanged(object sender, EventArgs e)
        {
            //如果选中并且没有密码就输入新密码
            string pwd=ConfigurationManager.AppSettings["admin"];
            if (ckbPwd.Checked && pwd == "")
            {
                FrmPws frmp = new FrmPws();
                frmp.ShowDialog();
            }
        }

        private void btnCreatePwd_Click(object sender, EventArgs e)
        {
            frmChangePwd frmchagepwd = new frmChangePwd();
            try
            {
                frmchagepwd.ShowDialog();
            }
            finally
            {
                frmchagepwd.Dispose();
            }
        }

        private void btnBrow_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = saveFileDialog1.FileName;
                try
                {
                    File.Copy(textBox1.Text, path);
                    textBox1.Text = path;
                    FrmPws.SetValue("database", path);
                    cuspath = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
